package com.senyint.sys.utils.pool;

import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

public abstract class PoolUseKey<K, T> {
	private final GenericKeyedObjectPool<K, T> internalPool;

	public PoolUseKey(KeyedPooledObjectFactory<K, T> factory, GenericKeyedObjectPoolConfig poolConfig) {
		this.internalPool = new GenericKeyedObjectPool<K, T>(factory, poolConfig);
	}

	public GenericKeyedObjectPool<K, T> getInternalPool() {
		return this.internalPool;
	}

	public T borrowObject(K key) {
		try {
			return this.internalPool.borrowObject(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void returnObject(K key, T resource) {
		this.internalPool.returnObject(key, resource);
	}

	public int idleSize() {
		return this.internalPool.getNumIdle();
	}

	public int activeSize() {
		return this.internalPool.getNumActive();
	}
}
